/**
 * @author Michael,Nov 21, 2012
 *
 */
package com.tm.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.tm.dao.TaskDAO;
import com.tm.pojo.Task;
import com.tm.service.TaskService;
import com.util.CommonResult;
import com.util.CommonResultMap;

/**
 * @author Michael
 *
 */
public class TaskServiceImpl implements TaskService {
	private TaskDAO taskDAO;
	
	
	public CommonResultMap setTaskEstimates(Task task){
		CommonResultMap map = CommonResultMap.getInstance();
//		if(task.getEstimateHour() != null && task.getUsersByAssignedUser() != null){
//			Calendar c = Calendar.getInstance();
//			Date workingDate = this.getUserLastWorkEndTime(task.getUsersByAssignedUser().getId());
//			workingDate.setTime(task.getEstimateStartTime().getTime());
//			c.setTime(workingDate);
//			c = DateUtil.getWorkingTime(c);
//			task.setEstimateStartTime(new Timestamp(c.getTime().getTime()));
//			task.setEstimateEndTime(DateUtil.addWorkingHours(task.getEstimateStartTime(), task.getEstimateHour()));			
//			//this.saveTask(task);
//			return ResultEnum.SUCCESS;
//		}else{
//			return ResultEnum.E_DATA;
//		}
		return map;
	}

	@Override
	public CommonResultMap saveTask(Task task) {
		CommonResultMap map = CommonResultMap.getInstance();
		if(StringUtils.isBlank(task.getTaskName())){
			map.setResult(CommonResult.ERROR, "Task name is empty");
			return map;
		}
		
		if(task.getTaskName().contains("#") && task.getTaskName().contains("\n")){
			String[] taskNames = task.getTaskName().split("\\n");
			Task[] tasks = new Task[taskNames.length];
			for (int i = 0; i < tasks.length; i++) {
				tasks[i] = new Task(taskNames[i]);
			}
			return this.saveTasks(tasks);
		}
		
		
		//1, find existing task
		Task oldTask = this.findByName(task.getTaskName());
		if(oldTask == null || oldTask.getDeleted().booleanValue() == true){
			taskDAO.saveOrUpdate(task);
			map.setResult(CommonResult.SUCCESS);
			map.setData(task);
		}else{
			map.setResult(CommonResult.ERROR, "task "+task.getTaskName()+" already exists");
		}
		return map;
	}

	@Override
	public CommonResultMap updateTask(Task task) {
		return null;
	}

	@Override
	public CommonResultMap deleteTask(Integer task) {
		return null;
	}

	@Override
	public List<Task> listTasks() {
		return taskDAO.listAll();
	}
	
	/**
	 * TODO:
	 * 查找用户被安排的最后一个TASK的结束时间
	 * @author Michael,Nov 21, 2012
	 * @param userId
	 * @return
	 */
	private Date getUserLastWorkEndTime(int userId){
		return new Date();
	}

	public TaskDAO getTaskDAO() {
		return taskDAO;
	}

	public void setTaskDAO(TaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}

	@Override
	public Task findByName(String taskName) {
		return taskDAO.findByName(taskName);
	}

	@Override
	public CommonResultMap saveTasks(Task[] tasks) {
			CommonResultMap map = CommonResultMap.getInstance();
			for(Task task: tasks){
				task.init();
				CommonResultMap map1 = this.saveTask(task);
				if(!map1.succeed()){
					map.addToResultList(CommonResult.ERROR, map1.getMessage());
				}
			}
			return map;
	}



}
